package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.common.util.UUIDUtil;
import com.softer.wxzj.entity.FileClassify;
import com.softer.wxzj.entity.FileClassifyDetail;
import com.softer.wxzj.mapper.FileClassifyDetailMapper;
import com.softer.wxzj.service.FileClassifyDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/7/6
 */
@Service
public class FileClassifyDetailServiceImpl implements FileClassifyDetailService {
    @Autowired
    private FileClassifyDetailMapper fileClassifyDetailMapper;

    /**
     * 新增图档类型
     *
     * @param fileClassifyDetail 图档类型
     * @return 新增结果
     */
    @Override
    public CommonResult add(FileClassifyDetail fileClassifyDetail) {
        fileClassifyDetail.setId(UUIDUtil.getUUID());
        String userName = ShiroUserUtil.getUserName();
        fileClassifyDetail.setCreater(userName);
        fileClassifyDetail.setModifier(userName);
        int num = fileClassifyDetailMapper.insert(fileClassifyDetail);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 获取业务分类下拉列表
     *
     * @param id 业务分类ID
     * @return 下拉列表
     */
    @Override
    public List<FileClassify> getClassifyList(String id) {
        return fileClassifyDetailMapper.getFileClassifyList(id);
    }

    /**
     * 查询图档详情
     *
     * @param id 图档ID
     * @return 详情
     */
    @Override
    public FileClassifyDetail getInfo(String id) {
        FileClassifyDetail fileClassifyDetail = fileClassifyDetailMapper.selectByPrimaryKey(id);
        //获取父级ID
        String parentId = getParentId(fileClassifyDetail);
        fileClassifyDetail.setFileClassifyId(parentId);
        return fileClassifyDetail;
    }

    /**
     * 查询类型的父级ID
     *
     * @param fileClassifyDetail 图档信息
     * @return 图档类型的全部级联ID
     */
    private String getParentId(FileClassifyDetail fileClassifyDetail) {
        List<String> list = new ArrayList<>(6);
        String classifyId = fileClassifyDetail.getFileClassifyId();
        while (!String.valueOf(NumberEnum.ZERO.getNum()).equals(classifyId)){
            list.add(0,classifyId);
            classifyId = fileClassifyDetailMapper.getParentId(classifyId);
        }
        String fileId = String.join(",", list);
        return fileId;
    }

    /**
     * 修改图档
     *
     * @param fileClassifyDetail 图档实体
     * @return 修改结果
     */
    @Override
    public CommonResult update(FileClassifyDetail fileClassifyDetail) {
        // 如果该类型已经上传过图片，禁止停用
        if (String.valueOf(NumberEnum.ZERO.getNum()).equals(fileClassifyDetail.getState())) {
            //查询该类型是否使用过
            int count = fileClassifyDetailMapper.getCountById(fileClassifyDetail.getId());
            if (count > 0) {
                return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "该类型已经使用，禁止停用");
            }
        }
        if (StringUtils.isEmpty(fileClassifyDetail.getFileClassifyId())){
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "请选择业务分类");
        }
        fileClassifyDetail.setModifier(ShiroUserUtil.getUserName());
        int num = fileClassifyDetailMapper.updateByPrimaryKey(fileClassifyDetail);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 获取业务分类的左侧三级菜单
     *
     * @return 三级菜单
     */
    @Override
    public FileClassify getFileClassifyList() {
        List<FileClassify> list = fileClassifyDetailMapper.getFileClassifyList(String.valueOf(NumberEnum.ZERO.getNum()));
        FileClassify fileClassify = list.get(NumberEnum.ZERO.getNum());
        List<FileClassify> secondList = fileClassifyDetailMapper.getFileClassifyList(fileClassify.getId());
        for (FileClassify f : secondList) {
            List<FileClassify> thirdList = fileClassifyDetailMapper.getFileClassifyList(f.getId());
            f.setChildren(thirdList);
        }
        fileClassify.setChildren(secondList);
        return fileClassify;
    }

    /**
     * 图档类型删除
     *
     * @param id ID
     * @return 删除结果
     */
    @Override
    public CommonResult delete(String id) {
        //查询该类型是否使用过
        int count = fileClassifyDetailMapper.getCountById(id);
        if (count > 0) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum(), "该类型已经使用，禁止删除");
        }
        int num = fileClassifyDetailMapper.deleteByPrimaryKey(id);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 图档列表查询
     *
     * @param page               分页实体
     * @param fileClassifyDetail 查询实体 必填字段id
     * @param fuzzy              模糊查询字段
     * @return 列表
     */
    @Override
    public PageInfo<FileClassifyDetail> getList(Page page, FileClassifyDetail fileClassifyDetail, String fuzzy) {
        String parentId = fileClassifyDetailMapper.getParentId(fileClassifyDetail.getId());
        //查询所传ID的级别，如果所传的ID是1，则查询所有，如果所传的ID的父级ID是1，则查询一个大类，否则只查询某个小类的数据
        if (String.valueOf(NumberEnum.ONE.getNum()).equals(fileClassifyDetail.getId())) {
            fileClassifyDetail.setId(null);
        } else if (String.valueOf(NumberEnum.ONE.getNum()).equals(parentId)) {
            //查询一个大类的ID集合
            String id = fileClassifyDetailMapper.getIdByParentId(fileClassifyDetail.getId());
            fileClassifyDetail.setId(id);
        }
        List<FileClassifyDetail> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isBlank(fuzzy)) {
            //精确查询
            list = fileClassifyDetailMapper.getList(fileClassifyDetail);
        } else {
            //模糊查询
            list = fileClassifyDetailMapper.fuzzySearch(fuzzy, fileClassifyDetail.getId());
        }
        for (FileClassifyDetail f : list) {
            int num = fileClassifyDetailMapper.getCountById(f.getId());
            if (num > NumberEnum.ZERO.getNum()) {
                f.setDeleteState(String.valueOf(NumberEnum.ZERO.getNum()));
                f.setDeleteMsg("该类型已经使用，禁止删除");
            } else {
                f.setDeleteState(String.valueOf(NumberEnum.ONE.getNum()));
            }
        }
        PageInfo<FileClassifyDetail> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
